package cn.i7baozh.starter.gateway.config;

import cn.i7baozh.starter.gateway.constant.GatewayConstants;
import cn.i7baozh.starter.gateway.handler.GlobalServiceInstanceExceptionHandler;
import cn.i7baozh.starter.gateway.loadbalancer.AvailableGroupLoadBalancer;
import cn.i7baozh.starter.gateway.loadbalancer.RoundRobinLoadBalancer;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

/**
 *
 * @author : baoqi.zhang
 * @date :   2022/5/17 20:04
 */
@Configuration
public class ApplicationContextConfig {

    /**
     * 统一错误信息输出
     * @param objectMapper
     * @see ErrorWebFluxAutoConfiguration#errorWebExceptionHandler(org.springframework.boot.web.reactive.error.ErrorAttributes, org.springframework.boot.autoconfigure.web.WebProperties, org.springframework.beans.factory.ObjectProvider, org.springframework.http.codec.ServerCodecConfigurer, org.springframework.context.ApplicationContext)
     * @return
     */
    @Bean
    @Order(-1)
    public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectMapper objectMapper) {

        return new GlobalServiceInstanceExceptionHandler(objectMapper);
    }

    /**
     * 模块负载均衡配置
     */
    @LoadBalancerClient(value = GatewayConstants.MODULE_STARTER_CLIENT, configuration = StarterClientLoadBalanceConfig.class)
    public class StarterClientLoadBalanceConfig{
        @Bean
        public ReactorLoadBalancer<ServiceInstance> availableGroupLoadBalancer(Environment environment,
                                                                               LoadBalancerClientFactory loadBalancerClientFactory) {
            String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
            return new AvailableGroupLoadBalancer(
                    loadBalancerClientFactory.getLazyProvider(name,
                            ServiceInstanceListSupplier.class),
                    name);
        }
    }

    /**
     * 模块负载均衡配置
     */
    @LoadBalancerClient(value = GatewayConstants.MODULE_STARTER_SERVICE, configuration = StarterServiceLoadBalanceConfigClient.class)
    public class StarterServiceLoadBalanceConfigClient{
        @Bean
        public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
                                                                                       LoadBalancerClientFactory loadBalancerClientFactory) {
            String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
            return new RoundRobinLoadBalancer(
                    loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
        }
    }
}
